package com.hdu.point_offer;

public class lcr155 {

    class Node {
        public int val;
        public Node left;
        public Node right;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right) {
            val = _val;
            left = _left;
            right = _right;
        }
    }

    class Solution {

        Node prev;
        Node head;
        public Node treeToDoublyList(Node root) {
            f(root);
            head.left = prev;
            prev.right = head;
            return head;
        }

        void f(Node root)
        {
            if (root == null) return;
            f(root.left);
            if (prev != null)
            {
                prev.right = root;
                root.left = prev;
            }
            if (head == null)
            {
                head = root;
            }
            prev = root;
            f(root.right);
        }
    }
}
